/**
 * @date 2005-12-27 14:07:01
 *
 */
package org.nanhill.xsheet;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.nanhill.xsheet.input.xhtml.DefaultXhtmlInputBuilder;
import org.nanhill.xsheet.input.xhtml.XhtmlInputBuilder;
import org.nanhill.xsheet.model.XSheetModel;

/**
 * @author alin
 * 
 */
public class XSheetHelper {
	//private static final Log logger = LogFactory.getLog(XSheetHelper.class);

	public static XSheetModel xml2Model(Document html)
			throws XSheetParseException {
		XhtmlInputBuilder inputBuilder = new DefaultXhtmlInputBuilder();
		inputBuilder.setDocument(html);
		try {
			inputBuilder.construct();
		} catch (Exception e) {
			throw new XSheetParseException(e);
		}
		return inputBuilder.getProduct();
	}

	public static XSheetModel xml2Model(String input)
			throws XSheetParseException {
		SAXReader reader = new SAXReader();
		Document doc;
		try {
			doc = reader.read(new ByteArrayInputStream(input.getBytes("utf8")));
		} catch (DocumentException e) {
			throw new XSheetParseException(e);
		} catch (UnsupportedEncodingException e) {
			throw new XSheetParseException(e);
		}
		return xml2Model(doc);
	}
}
